package com.jxzn.locust.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import com.jxzn.locust.support.LoginSession;

@Component
public class LoginInterceptorHandler implements HandlerInterceptor {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();
        if (null == session.getAttribute(LoginSession.USER_ID.name()) || StringUtils.isBlank(session.getAttribute(LoginSession.USER_ID.name()).toString())) {
            response.sendRedirect("/login.html");
            return false;
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

}
